#include "FT5316.h"


#define delay_ms(x) HAL_Delay(x)

static struct _ts_event ts_event = {0};


static void FT5316_reset(void)
{
	FT5316_RSTN_RESET;
	delay_ms(5);
	FT5316_RSTN_SET;
	delay_ms(350);
}


void Int_FT5316_ReadBuf(uint8_t innerAddr, uint8_t *buf, uint8_t size)
{
    Dri_I2C_Start();
    Dri_I2C_WriteByte(FT5316_ADDRESS << 1);
    Dri_I2C_Wait4Nack();
    Dri_I2C_WriteByte(innerAddr);
    Dri_I2C_Wait4Nack();
    Dri_I2C_Start();
    Dri_I2C_WriteByte((FT5316_ADDRESS<<1)+ 1);
    Dri_I2C_Wait4Nack();
    for (uint8_t i = 0; i < size; i++)
    {
        buf[i] = Dri_I2C_ReadByte();
        if (i < size - 1)
        {
            Dri_I2C_Ack();
        }
        else
        {
            Dri_I2C_Nack();
        }
    }
    Dri_I2C_Stop();
}


static void FT5316_RdParFrPCTPFun(uint8_t *PCTP_Par,uint8_t ValFlag)
{  
  
  Int_FT5316_ReadBuf(0,PCTP_Par,ValFlag);
}


bool FT5316_isInterrupt(void)
{
 	if(FT5316_INT_READ)      //Detect the occurrence of an interrupt
 	{
		ts_event.Key_Sta = KEY_DOWN;
		
    return true;
 	}
	
  return false;
}


void FT5316_init(void)
{
		RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
    GPIOC->CRL |= GPIO_CRL_MODE2;
    GPIOC->CRL &= ~GPIO_CRL_CNF2;
		//INT 
		GPIOC->CRL &=~ GPIO_CRL_CNF1_1;
		GPIOC->CRL |=	GPIO_CRL_CNF1_0;
		GPIOC->CRL &= ~GPIO_CRL_MODE1;
		Dri_I2C_Init();
		FT5316_reset();
}


void FT5316_Wr_Reg(uint8_t RegIndex, uint8_t RegValue1)
{  
		Dri_I2C_Start();
    Dri_I2C_WriteByte(FT5316_ADDRESS << 1);
    // �ȶԷ�Ӧ��
    Dri_I2C_Wait4Nack();

    Dri_I2C_WriteByte(RegIndex);
    Dri_I2C_Wait4Nack();
    Dri_I2C_WriteByte(RegValue1);
    Dri_I2C_Wait4Nack();

    Dri_I2C_Stop();

    Delay_ms(1);
}


uint8_t FT5316_Read_Reg(uint8_t RegIndex)
{
    uint8_t receive=0;
    Dri_I2C_Start();
    Dri_I2C_WriteByte(FT5316_ADDRESS << 1);
    Dri_I2C_Wait4Nack();
    Dri_I2C_WriteByte(RegIndex);
    Dri_I2C_Wait4Nack();
    Dri_I2C_Start();
    Dri_I2C_WriteByte((FT5316_ADDRESS << 1) +1);
    Dri_I2C_Wait4Nack();
    receive = Dri_I2C_ReadByte();
    Dri_I2C_Nack();
    Dri_I2C_Stop();
	  return receive;
}


uint8_t FT5316_touchDataRead(void)
{
  uint8_t buf[32] = {0}; uint8_t ret = 0;

	#ifdef CONFIG_FT5X0X_MULTITOUCH
		FT5316_RdParFrPCTPFun(buf, 31);
	#else
  	FT5316_RdParFrPCTPFun(buf, 7);
	#endif

  ts_event.touch_point = buf[2] & 0xf;

  if (ts_event.touch_point == 0) 
	{
		return 0;
  }

	#ifdef CONFIG_FT5X0X_MULTITOUCH
	switch (ts_event.touch_point) 
	{
    case 5:
      ts_event.x5 = (uint32_t)(buf[0x1b] & 0x0F)<<8 | (uint32_t)buf[0x1c];
      ts_event.y5 = (uint32_t)(buf[0x1d] & 0x0F)<<8 | (uint32_t)buf[0x1e];
    
    case 4:
      ts_event.x4 = (uint32_t)(buf[0x15] & 0x0F)<<8 | (uint32_t)buf[0x16];
      ts_event.y4 = (uint32_t)(buf[0x17] & 0x0F)<<8 | (uint32_t)buf[0x18];
    
    case 3:
      ts_event.x3 = (uint32_t)(buf[0x0f] & 0x0F)<<8 | (uint32_t)buf[0x10];
      ts_event.y3 = (uint32_t)(buf[0x11] & 0x0F)<<8 | (uint32_t)buf[0x12];
    
    case 2:
      ts_event.x2 = (uint32_t)(buf[9] & 0x0F)<<8 | (uint32_t)buf[10];
      ts_event.y2 = (uint32_t)(buf[11] & 0x0F)<<8 | (uint32_t)buf[12];
    
    case 1:
      ts_event.x1 = (uint32_t)(buf[3] & 0x0F)<<8 | (uint32_t)buf[4];
      ts_event.y1 = (uint32_t)(buf[5] & 0x0F)<<8 | (uint32_t)buf[6];
      break;
    
    default:
      return 0;
	}
	#else
	if (ts_event.touch_point == 1)
	{
	 	ts_event.x1 = (uint32_t)(buf[3] & 0x0F)<<8 | (uint32_t)buf[4];
	 	ts_event.y1 = (uint32_t)(buf[5] & 0x0F)<<8 | (uint32_t)buf[6];
	 	ret = 1;
	}
	else
	{
    ts_event.x1 = 0xFFFF;
	 	ts_event.y1 = 0xFFFF;
	 	ret = 0;
	}
	#endif
  
	return ret;
}




bool FT5316_Get_xy(unsigned short* x, unsigned short* y)
{
  *x = 320-ts_event.x1 ;
  *y = ts_event.y1 ;
  if(ts_event.Key_Sta == KEY_DOWN)
  {
    ts_event.Key_Sta = KEY_UP;
    return true;
  }
  return false;
}
